package com.alibaba.manager;

import com.alibaba.common.SystemConstants;
import com.alibaba.define.domain.UserDO;
import com.alibaba.mapper.UserMapper;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

import static com.alibaba.common.SystemConstants.DEFAULT_AVATAR;
import static com.alibaba.common.SystemConstants.UN_DELETED;


/**
 * @author : hanyang
 * @date : 2023-11-03 23:42
 **/

@Component
public class UserManager {

    @Resource
    UserMapper userDAO;

    /**
     * 根据用户名查询用户
     */
    public UserDO selectUserByUserName(String userName) {
        HashMap<String, Object> conditionMap = new HashMap<>();
        conditionMap.put("userName", userName);
        conditionMap.put("status", SystemConstants.AVAILABLE);
        conditionMap.put("delFlag", UN_DELETED);
        List<UserDO> userDOS = userDAO.select(conditionMap);
        return Optional.ofNullable(userDOS)
                .filter(users -> !users.isEmpty())
                .map(users -> users.get(0))
                .orElse(null);
    }

    /**
     * 根据用户Id查询用户昵称
     */
    public String selectUserNicknameById(Long id) {
        List<UserDO> userDOS = this.selectUserListById(id);
        return Optional.ofNullable(userDOS)
                .filter(users -> !users.isEmpty())
                .map(users -> users.get(0).getNickName())
                .orElse(null);
    }

    /**
     * 根据用户id查询用户
     */
    public UserDO selectUserById(Long id) {
        List<UserDO> userDOS = this.selectUserListById(id);
        return Optional.ofNullable(userDOS)
                .filter(users -> !users.isEmpty())
                .map(users -> users.get(0))
                .orElse(null);
    }

    /**
     * 根据用户id查询用户
     */
    public List<UserDO> selectUserListById(Long id) {
        HashMap<String, Object> conditionMap = new HashMap<>();
        conditionMap.put("id", id);
        conditionMap.put("status", SystemConstants.AVAILABLE);
        conditionMap.put("delFlag", UN_DELETED);
        return userDAO.select(conditionMap);
    }

    /**
     * 批量查询用户id+用户昵称
     */
    public Map<Long, UserDO> batchQueryUserDOS(Set<Long> userIds) {
        Map<Long, UserDO> idUserDOMap = new HashMap<>();
        if (CollectionUtils.isEmpty(userIds)) {
            return idUserDOMap;
        }
        List<UserDO> userDOS = userDAO.selectUserDOByIds(userIds);
        idUserDOMap =  userDOS.stream().collect(Collectors.toMap(
                UserDO::getId,
                Function.identity()
        ));
        return idUserDOMap;
    }

    /**
     * 更新用户信息
     */
    public boolean updateUserInfo(UserDO userDO) {
        // 判断userDO
        if (Objects.isNull(userDO.getId())) {
            return false;
        }
        return Objects.equals(userDAO.updateUserInfo(userDO), 1);
    }

    /**
     * 注册前校验用户名是否存在
     */
    public boolean isUsernameExist(String userName) {
        HashMap<String, Object> conditionMap = new HashMap<>();
        conditionMap.put("userName", userName);
        conditionMap.put("delFlag", UN_DELETED);
        List<UserDO> userDOS = userDAO.select(conditionMap);
        return CollectionUtils.isNotEmpty(userDOS);
    }

    /**
     * 注册前校验邮箱是否已被占用
     */
    public boolean isEmailExist(String email) {
        HashMap<String, Object> conditionMap = new HashMap<>();
        conditionMap.put("delFlag", UN_DELETED);
        conditionMap.put("email", email);
        List<UserDO> userDOS = userDAO.select(conditionMap);
        return CollectionUtils.isNotEmpty(userDOS);
    }

    /**
     * 用户注册
     */
    public boolean userRegister(UserDO userDO) {
        // 校验
        if (Objects.isNull(userDO)) {
            return false;
        }
        return Objects.equals(userDAO.insert(userDO), 1);
    }
}

